<?
include "include/config.php";
	
$tip=array ('1'=>'Garsoniera','2'=>'Apartament 2 camere','3'=>'Apartament 3 camere','4'=>'Apartament 4 camere','5'=>'Teren','6'=>'Spatiu Comercial','7'=>'Casa/Vila');
$oferta=array ('1'=>'Vanzare','2'=>'Inchiriere');
$smarty->assign('tip',$tip);
$smarty->assign('oferta',$oferta);

$user_mod_query=mysql_query("select id,name from users where level=10 ");
$agents["0"]="Alegeti agent";	
while ($users=mysql_fetch_array($user_mod_query)) {
    $agents[$users["id"]] =$users["name"];
}
$smarty->assign('agent',$agents);

    if ($_POST['trimite']!='') {

	$Tranz = $_REQUEST["tranzactie"]; 
	$Ofert = $_REQUEST["oferta"];
	$N = $_REQUEST["nume"];
	$T = $_REQUEST["telefon"];
	
	$M = $_REQUEST["mesaj"]; 
	$YM = $_REQUEST["email"];
	$user_id = $_REQUEST["user_id"];
	list($email)=mysql_fetch_array(mysql_query("select email from users where id='".$user_id."'"));
	$agentEmail=$email;
		
	if (strlen($N)>3) $bineN = 1 ; else {$bineN=0; $eroare="Completati Numele";}
	if ((isset($T)) AND (is_numeric($T))) $bineT = 1 ; else  {$bineT=0;     $eroare="Completati Numarul de Telefon";} 	
	if (strlen($M)>3) $bineM = 1 ; else  { $bineM=0; $eroare="Completati Casuta de Mesaje";}    ;      		  
	$rezultat = $bineN*$bineT*$bineM;
 
	$errors=0;
    
	$number = $_POST['number'];

	if(md5(strtoupper($number)) != $_SESSION['image_value']) {
	    $eroare= 'Cod gresit !';
	}
 
	if (($rezultat==1) AND ($eroare=='')) { 
	    $introdu = " <strong>Nume :</strong>".$N." <strong>Telefon : </strong> ".$T." <strong><br>Mesaj : </strong>".$M." <strong> <br> Email : </strong>".$YM;           
	    mysql_query( "INSERT INTO mesaje ( nume ,descriere, data,telefon ,email,comentarii,mesaj,tip_mesaj,user_id) VALUES ('".$N."', '".$introdu."',date(NOW()),'".$T."','".$YM."','','<b>".$Ofert."</b> ".$M."','".$Tranz."','{$user_id}')" ) ;        
		$smarty->assign ('trimis',1);
	}   
	//se creeaza un e-mail de alerta si se trimit datele. 
	$to ="office@timonatrust.ro" . " ,";
	$to .="dan@timonatrust.ro";
	if($agentEmail!="") {
	    $to .=", {$agentEmail}";
	}
		
       // $to ="orionsky30@gmail.com";    
	$subiect = "CEREREA TA!";
	$mesaj= '<html>
				<head><title>Cererea Ta!</title></head>
			<body>
				<b><font color="#333333" face="tahoma">'.$tip[$Tranz] . ' ' . $Ofert.'</font><b><br><br>
				<font color="#999900">'.$introdu.'</font><br><br><br><br>
				<div align="center">This is a message from http://www.timona.ro</div>
			</body>					
			</html>';
	$headers = "MIME-Version: 1.0\n";
	$headers .= 'Content-type: text/html; charset=iso-8859-1' ."r\n";
	$headers .= "From: ".$N." <".$YM.">\n";
	@mail($to, $subiect, $mesaj, $headers);

	}

 $smarty->assign ('eroare',$eroare);   
$smarty->assign("content", $smarty->fetch(tmpd.'cererea_ta'.tmpl_ext));
include "include/make_page.php";
?>